# 导包
from sklearn.metrics import roc_auc_score, roc_curve
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report
from sklearn.metrics import roc_auc_score, roc_curve

# 加载数据
data = pd.read_csv(r'D:\DevelopSoftware\bj-GIT_data_mining_william\data\train.csv')

# 处理分类特征
X = data.drop('Attrition', axis=1)
y = data['Attrition']

# One-Hot编码分类变量
categorical_cols = X.select_dtypes(include=['object']).columns  # 选择分类变量 选择数据框X中所有的 object , columns 拿取所有object的列
X_encoded = pd.get_dummies(X, columns=categorical_cols)  # 使用pandas 中的所有 get_dummies 对x列进行 One-Hot编码分类变量

# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(
    X_encoded, y, test_size=0.2, random_state=42
)

# 训练带剪枝的决策树
model = RandomForestClassifier(
    class_weight='balanced',
    max_depth=5,
    min_samples_split=20,
    random_state=32
)
# 训练模型
model.fit(X_train, y_train)

# 评估模型
y_pred = model.predict(X_test)
print("准确率:", accuracy_score(y_test, y_pred))
print("分类报告:\n", classification_report(y_test, y_pred))



y_proba = model.predict_proba(X_test)[:, 1]
auc = roc_auc_score(y_test, y_proba)
print(f"AUC: {auc:.3f}")


